from twisted.application import service
from twisted.internet import reactor
from twisted.plugin import getPlugins

from pyhsrv import pyhsrvservice, interfaces, net, db, fakenodeservice
import ConfigParser

def getConf(config):
    cfg = ConfigParser.SafeConfigParser()
    cfg.read(config)
    return cfg

def PyHSrvServiceMaker(config):
    cfg = getConf(config)
    s = service.MultiService()
    for i in getPlugins(interfaces.IDBPlugin, db):
        datab = i.getdb(cfg.get('general', 'dburl'))
        controller = pyhsrvservice.PyHSrvService(cfg.get('general',\
                                                    'servername'), datab, cfg)
        for i in getPlugins(interfaces.INetAlertPlugin, net):
            if i.getName() in cfg.get('alert','protocols').split(','):
                x = i.makeService(controller, cfg)
                x.setServiceParent(s)
        fns = fakenodeservice.FakeNodeService(datab, cfg.get('general',\
                                                             'servername'))
        for i in getPlugins(interfaces.IFakeNodeNetPlugin, net):
            if i.getName() in cfg.get('net', 'protocol').split(','):
                x = i.makeService(fns, cfg)
                x.setServiceParent(s)
    return s

